package dao;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import ehcache.EHCacheUtil;

import entity.User;

public class UserDAO {

	private SqlSessionFactory sqlSessionFactory = null;

	public Integer AddUser(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			return (Integer) session.selectOne("entity.User.insertUser", user);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			session.close();
		}
	}

//	/此方法不适合用缓存
	public Integer GetUserCountByLoginName(String loginName) {
//		String cacheName = "GetUserCountByLoginName(" + loginName + ")";
//		Integer c = (Integer)EHCacheUtil.GetFromCache(cacheName);
//		if(c != null)
//			return c;
		
		SqlSession session = sqlSessionFactory.openSession();
		try {
			Integer result = (Integer) session.selectOne("entity.User.selectUserCountByLoginName", loginName);
//			EHCacheUtil.SetCache(cacheName, result);
			return result;
			
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			session.close();
		}
	}

	public Integer GetUserIdByLoginName(String loginName) {
		String cacheName = "GetUserIdByLoginName(" + loginName + ")";
		Integer c = (Integer)EHCacheUtil.GetFromCache(cacheName);
		if(c != null)
			return c;
		
		SqlSession session = sqlSessionFactory.openSession();

		try {
			Integer result = (Integer) session.selectOne("entity.User.selectUserIdByLoginName", loginName);
			EHCacheUtil.SetCache(cacheName, result);
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public String GetNickNameByID(int userid){
		String cacheName = "GetNickNameByID(" + userid + ")";
		String c = (String)EHCacheUtil.GetFromCache(cacheName);
		if(c != null)
			return c;
		
		
		SqlSession session = sqlSessionFactory.openSession();

		try {
			String nickName = (String)session.selectOne("entity.User.selectNickNameByID", userid);
			EHCacheUtil.SetCache(cacheName, nickName);
			
			return nickName;
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null)
				session.close();
		}

	}
	
	public String GetHeartByID(int userid){

		
		SqlSession session = sqlSessionFactory.openSession();

		try {

			
			return (String)session.selectOne("entity.User.selectHeartByID", userid);
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null)
				session.close();
		}

	}
	
	public User GetUserByID(int userid) throws IOException {

		SqlSession session = sqlSessionFactory.openSession();
		User user = null;
		try {
			user = (User) session.selectOne("entity.User.selectUserByID", userid);
			// System.out.println(user.getYwsNickName());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
		return user;
	}


	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}
}
